package DLSched;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.File;
import java.util.ArrayList;

public class ParseHtml
{

    /**
     * http://jsoup.org/cookbook/extracting-data/example-list-links
     * ^^This was used as example
     */
    public static ArrayList<String> get_links(String filepath)
    {
        ArrayList<String> possible_links = new ArrayList();
        try
        {
            Document doc = Jsoup.parse(new File(filepath), null);
            Elements links = doc.select("a[href]");
            Elements media = doc.select("[src]");
            Elements imports = doc.select("link[href]");

            print("\nMedia: (%d)", media.size());
            for (Element src : media)
            {
                if (src.tagName().equals("img"))
                {
                    print(" * %s: <%s> %sx%s (%s)",
                            src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
                            trim(src.attr("alt"), 20));
                }
                else
                {
                    print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
                }
            }

            print("\nImports: (%d)", imports.size());
            for (Element link : imports)
            {
                print(" * %s <%s> (%s)", link.tagName(), link.attr("abs:href"), link.attr("rel"));
            }

            print("\nLinks: (%d)", links.size());
            for (Element link : links)
            {
                print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
                possible_links.add(link.attr("abs:href"));
            }
            return possible_links;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    private static void print(String msg, Object... args)
    {
        System.out.println(String.format(msg, args));
    }

    private static String trim(String s, int width)
    {
        if (s.length() > width)
        {
            return s.substring(0, width - 1) + ".";
        }
        else
        {
            return s;
        }
    }

    public static void main(String[] args) throws IOException
    {
    }
}
